package servlet;

import com.alibaba.fastjson.JSON;
import dao.UserDao;
import pojo.Response;
import util.SendEmailUtil;
import util.VerifiCode;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/emailServlet")
public class EmailServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");
        System.out.println("email get...");
        String email = req.getParameter("email");
        System.out.println(email);
        PrintWriter writer = resp.getWriter();
        VerifiCode verifiCode = new VerifiCode();
        String code = verifiCode.getC();
        System.out.println("your code is:" + code);
        //boolean b =SendEmailUtil.sendEmail(email,code);
        boolean b = true;
        if (b) {
            writer.write(JSON.toJSONString(new Response(null, "发送成功", code)));
        } else {
            writer.write(JSON.toJSONString(new Response(null, "发送失败", null)));
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("email post...");
        this.doGet(req, resp);
    }
}
